<!--
 * @Author: zhangwei 1405298045@qq.com
 * @Date: 2022-02-22 10:50:03
 * @LastEditTime: 2022-04-24 09:54:16
 * @LastEditors: zhangwei 1405298045@qq.com
 * @Description: 
 * @FilePath: /ll-fe-mp-dyfx/src/App.vue
 *  
-->
<script>
import srorageKey from '@/utils/srorage/key';
import { isIos, getLocal, setLocal } from '@/utils';
//  #ifdef MP-WEIXIN
import { collectSdk } from '@/dataCollect';

collectSdk.initSdk();
//  #endif

export default {
	onLaunch() {
		uni.hideTabBar();

		// #ifndef MP-WEIXIN
		const isAgreement = getLocal(srorageKey.userAgreement);

		if (!isAgreement) {
			uni.reLaunch({
				url: '/setupPage/Agreement'
			});
			return;
		}

		// #ifdef APP-PLUS
		const clientid = getLocal(srorageKey.pushClientid);

		if (!clientid) {
			const clientInfo = plus.push.getClientInfo();
			setLocal(srorageKey.pushClientid, clientInfo.clientid);
		}

		plus.push.addEventListener('click', (params) => {
			const { aps, payload } = params;

			this.changeBadgeNumber('click');

			if (!aps) {
				const { id, type } = typeof payload === 'object' ? payload : JSON.parse(payload);
			} else {
				const {
					alert: { otherArgs }
				} = aps;

				const { id, type } = typeof otherArgs === 'object' ? otherArgs : JSON.parse(otherArgs);
			}
		});

		plus.push.addEventListener('receive', (params) => {
			const { type, payload } = params;

			if (isIos() && !type) {
				return;
			}

			this.changeBadgeNumber();

			const { title, body, otherArgs } = typeof payload === 'object' ? payload : JSON.parse(payload);
			const formatOtherArgs = typeof otherArgs === 'object' ? JSON.stringify(otherArgs) : otherArgs;

			plus.push.createMessage(body, formatOtherArgs, {
				title
			});
		});
		// #endif

		// #endif

		// #ifdef MP-WEIXIN
		const version = getLocal(srorageKey.weixinVersion);
		if (!version) {
			uni.clearStorageSync();

			setLocal(srorageKey.weixinVersion, '2.1.0');
		}
		// #endif
	},

	onShow() {
		// #ifdef MP-WEIXIN
		const updateManager = uni.getUpdateManager();
		updateManager.onCheckForUpdate((res) => {
			if (!res.hasUpdate) {
				return;
			}

			// 请求完新版本信息的回调
			updateManager.onUpdateReady((res) => {
				uni.showModal({
					title: '更新提示',
					showCancel: false,
					content: '新版本已经准备好，是否重启应用？',
					success(res) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						res.confirm && updateManager.applyUpdate();
					}
				});
			});

			updateManager.onUpdateFailed(() => {
				// 新的版本下载失败
				uni.showModal({
					title: '已经有新版本了哟~',
					content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
				});
			});
		});

		this.setProfile();
		// #endif
	},

	methods: {
		// #ifdef MP-WEIXIN
		getCollectSdk() {
			return collectSdk;
		},
		// #endif

		getUuid() {
			const formatUuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
				const r = (Math.random() * 16) | 0;
				const v = c == 'x' ? r : (r & 0x3) | 0x8;

				return v.toString(16);
			});

			return `DYFX_${formatUuid}`;
		},

		setProfile() {
			let userUuid = getLocal(srorageKey.DYFX_user_uuid);
			const { locationIdStr = 1 } = getLocal(srorageKey.locationId) || {};

			if (!userUuid) {
				const uuid = this.getUuid();
				setLocal(srorageKey.DYFX_user_uuid, uuid);

				userUuid = uuid;
			}

			setTimeout(() => {
				getApp().getCollectSdk().setProfile({ uid: userUuid, source: 5, location_id: locationIdStr });
			});
		},

		changeBadgeNumber(type) {
			if (!isIos()) {
				return;
			}

			const UIApplication = plus.ios.import('UIApplication');
			const app = UIApplication.sharedApplication();
			const oldNum = app.applicationIconBadgeNumber() || 0;

			const newOldNum = oldNum + (type ? -1 : 1);

			// 设置应用图标数量
			plus.runtime.setBadgeNumber(newOldNum);

			// 导入个推原生累
			const GetuiSdk = plus.ios.importClass('GeTuiSdk');
			GetuiSdk.setBadge(newOldNum);
		}
	}
};
</script>

<style lang="scss">
page {
	background: #f8f8f8;
	-webkit-overflow-scrolling: touch;
}

view,
input,
button,
textarea {
	box-sizing: border-box;
}

/** 去掉默认image底部的5px距离 */
image {
	vertical-align: bottom;
}

/** 隐藏 ios15搜索框自带搜索图标 */
[type='search']::-webkit-search-decoration {
	display: none;
}
</style>
